from fastapi import APIRouter,Depends,Request,HTTPException, status, Query
from novelapp.config.database import connect
from novelapp.models.models import Config,Notice
from novelapp.common.common import returnData
import json

router = APIRouter()  

@router.get("/get_config/")
async def get_config():
    try:
        # 获取 Config 表数据  test
        config_data = Config.objects.first()
        if not config_data:
            return returnData(404, 'Config data not found', data=None)

        # 获取 Notice 表中最新的一条数据
        notice_data = Notice.objects.order_by('-createdAt').first()
        if not notice_data:
            return returnData(404, 'Notice data not found', data=None)
            
        notice_data_json =  json.loads(notice_data.to_json())
        notice_data_json['createdAt'] = notice_data.createdAt.strftime('%Y-%m-%d %H:%M:%S')
        
        return returnData(200, 'Success', data={
            'config': json.loads(config_data.to_json()),
            'notice': notice_data_json,
            'pinglun': 1,
        })
    except Exception as e:
        return returnData(500, f'Error fetching data: {str(e)}', data=None)  